<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div id="app">
      <div>
        <span>姓名:</span>
        <input type="text" v-model="newStudent.name" />
      </div>
      <div>
        <span>年龄:</span>
        <input type="number" v-model="newStudent.age" />
      </div>
      <div>
        <span>性别:</span>
        <select v-model="newStudent.sex">
          <option value="男">男</option>
          <option value="女">女</option>
        </select>
      </div>
      <div>
        <button @click="add">添加/修改</button>
      </div>
      <div>
        <table border="1" cellpadding="10" cellspacing="0">
          <tr>
            <th>序号</th>
            <th>姓名</th>
            <th>年龄</th>
            <th>性别</th>
            <th>操作</th>
          </tr>
          <tr v-for="(item, index) in list":key="index">
            <td>{{index+1}}</td>
            <td>{{item.name}}</td>
            <td>{{item.age}}</td>
            <td>{{item.sex}}</td>
            <td>
              <button @click="del(index)">删除</button>
              <button @click="edit(index)">编辑</button>
            </td>
          </tr>
        </table>
      </div>
  </div>
  <script src="./vue.js"></script>
  <script>
    /* 
    - 需求1: 铺设页面, 准备初始的数据(自己手写数据结构) - 前面是数组索引+1 *作为序号
    - 需求2: 当输入框没有值, 要给用户一个提示, 必须都有值才能增加新数据 (数据驱动页面哦)
    - 需求3: 添加功能 - 想好数据结构统一对象的key
    - 需求4: 点击编辑功能, 把值赋予到输入框上(不要操作dom, 数据驱动页面)
    - 需求5: 用户修改后, 点击相同按钮 - 想想怎么判断怎么是添加还是修改的功能 (提示: 准备一个全局变量, 点过编辑按钮可以让它为true) - 实现编辑后更新页面效果
    - 需求6: 点击删除, 删除这行数据
    */
    const app = new Vue({
      el: '#app',
      isChange: false,
      data: {
        list: [
            { id: 1, name: '张三', age: 18, sex: '男' },
            { id: 2, name: '李四', age: 20, sex: '女' },
            { id: 3, name: '王五', age: 22, sex: '男' }
        ],
        newStudent: {
          id: null,
          name: '',
          age: '',
          sex: '男'
        },
        isEdit: false,
        editIndex: null
      },
      methods: {
            add () {
              if (this.isEdit) {
                this.list[this.editIndex] = {
                  id: this.list[this.editIndex].id,
                  name: this.newStudent.name,
                  age: this.newStudent.age,
                  sex: this.newStudent.sex
                }
              }
              if (this.newStudent.name && this.newStudent.age) {
                  this.newStudent.id = + new Date()
                  this.list.push(this.newStudent)
                  // 清空表单
                  this.newStudent = {
                      id: null,
                      name: '',
                      age: '',
                      sex: '男'
                  }
              }
            },
            edit (index) {
                this.newStudent = this.list[index]
                this.isEdit = true
                this.editIndex = index
            },
            del (index) {
                this.list.splice(index, 1)
            }

        }
    })
  </script>
</body>

</html>